package com.mall.controller;


import cn.hutool.core.map.MapUtil;
import com.github.pagehelper.PageInfo;
import com.mall.common.model.GlobalResponse;
import com.mall.consts.MallConst;
import com.mall.form.OrderCreateForm;
import com.mall.pojo.User;
import com.mall.service.IOrderService;
import com.mall.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.lang.ref.ReferenceQueue;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class OrderController{

    @Autowired
    IOrderService orderService;

    @PostMapping("/orders")
    public GlobalResponse<OrderVO> create(@Valid @RequestBody OrderCreateForm form,
                                      HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.create(user.getId(), form.getShippingId());
    }

    @GetMapping("/orders")
    public GlobalResponse<PageInfo> list(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         HttpSession session) {

        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.list(user.getId(), pageNum, pageSize);
    }

    @GetMapping("/orders/{orderNo}")
    public GlobalResponse<OrderVO> detail(@PathVariable Long orderNo,
                                      HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        return orderService.detail(user.getId(), orderNo);
    }

    @PutMapping("/orders/{orderNo}")
    public GlobalResponse cancel(@PathVariable Long orderNo,
                             HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return orderService.unOrder(user.getId(), orderNo);
    }
}
